package cn.aitrox.ry.service.config.service.impl;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.config.dao.ComponentDao;
import cn.aitrox.ry.service.config.dto.in.ComponentCreateInDto;
import cn.aitrox.ry.service.config.dto.in.ComponentEditInDto;
import cn.aitrox.ry.service.config.entity.ComponentEntity;
import cn.aitrox.ry.service.config.service.ComponentService;
import cn.aitrox.ry.util.CodeUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ComponentServiceImpl implements ComponentService {

    @Autowired
    private ComponentDao componentDao;

    @Override
    public ComponentEntity read(Integer id) {
        return componentDao.findById(id);
    }

    @Override
    public PageInfo<ComponentEntity> search(Integer page, Integer pageSize, List<Integer> ids, String name) {
        String likeName = null;
        if (StringUtils.isNotEmpty(name)) {
            likeName = "%" + name + "%";
        }
        return componentDao.search(page, pageSize, ids, likeName);
    }

    @Transactional
    @Override
    public RespResult create(ComponentCreateInDto input) {
        if (nameExisted(null, input.getName())) {
            return RespResult.secByError(RespCodeEnum.EXISTED.getCode(), "名称已存在");
        }

        ComponentEntity componentEntity = new ComponentEntity(null, null, input.getName(), input.getType(), input.getParameter(), input.getData(), true);
        componentDao.insertSelective(componentEntity);
        Integer id = componentEntity.getId();
        String code = CodeUtil.getNumberCode(id, 6);

        ComponentEntity update = new ComponentEntity(id, code, null, null, null, null, null);
        componentDao.updateByIdSelective(update);

        return RespResult.ok();
    }

    @Transactional
    @Override
    public RespResult edit(ComponentEditInDto input) {
        // 校验是否可编辑
        ComponentEntity componentEntity = componentDao.findById(input.getId());
        if (null == componentEntity) {
            return RespResult.error("数据不存在");
        }

        if (nameExisted(input.getId(), input.getName())) {
            return RespResult.secByError(RespCodeEnum.EXISTED.getCode(), "名称已存在");
        }

        ComponentEntity update = new ComponentEntity(input.getId(), null, input.getName(), input.getType(), input.getParameter(), input.getData(), null);

        componentDao.updateByIdSelective(update);

        return RespResult.ok();
    }

    private Boolean nameExisted(Integer id, String name) {
        ComponentEntity cond = new ComponentEntity();
        cond.setName(name);
        List<ComponentEntity> listByName = componentDao.findByCondition(cond, null);
        return listByName.stream().filter(x -> !x.getId().equals(id) && x.getName().equals(name)).count() > 0;
    }
}
